package com.zcyr.core.sql.visitor;

import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor;
import com.zcyr.core.sql.statement.OGCreateSchemaStatement;
import com.zcyr.core.sql.statement.OGDropSchemaStatement;

/**
 * 适用于opengauss的AST语法树转字符串visitor
 * 遍历AST语法树并转换为字符串输出, 大多数还是依赖pg的visitor
 */
public class OGOutputVisitor extends PGOutputVisitor implements OGASTVisitor{
    public OGOutputVisitor(Appendable appender) {
        super(appender);
    }

    public boolean visit(OGDropSchemaStatement x) {
        print0(ucase ? "DROP SCHEMA " : "schema database ");

        if (x.isIfExists()) {
            print0(ucase ? "IF EXISTS " : "if exists ");
        }
        StringBuilder schemas = new StringBuilder();
        for(int i=0;i<x.getSchemas().size();i++){
            schemas.append(x.getSchemas().get(i).getSimpleName());
            schemas.append(",");
        }
        schemas.deleteCharAt(schemas.length() - 1);
        print0(schemas.toString());
        return false;
    }

    @Override
    public boolean visit(PGSelectStatement x) {
        return super.visit(x);
    }

    @Override
    public boolean visit(OGCreateSchemaStatement x) {
        print0(ucase ? "CREATE SCHEMA " : "create schema ");
        if (x.isIfNotExists()) {
            print0(ucase ? "IF NOT EXISTS " : "if not exists ");
        }
        x.getSchema().accept(this);

        if (x.getCharacterSet() != null) {
            print0(ucase ? " CHARACTER SET " : " character set ");
            print0(x.getCharacterSet());
        }

        if (x.getCollate() != null) {
            print0(ucase ? " COLLATE " : " collate ");
            print0(x.getCollate());
        }

        return false;
    }

}
